define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'activtyc/guess/index',
                    add_url: 'activtyc/guess/add',
                    edit_url: 'activtyc/guess/edit',
                    del_url: 'activtyc/guess/del',
                    multi_url: 'activtyc/guess/multi',
                    byOut_url:'activtyc/guess/by_out',
                    table: 'guess',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'out_trade_no', title: __('商户订单号')},
                        {field: 'url', title: __('Url'), formatter: Controller.api.formatter.avatar},
                        {field: 'name', title: __('Name')},
                        {field: 'phone', title: __('Phone')},
                        {field: 'shen_text', title: __('Shen'),operate: false},
                        {field: 'shi_text', title: __('Shi'),operate: false},
                        {field: 'xian_text', title: __('Xian'),operate: false},
                        {field: 'address', title: __('Address')},
                        {field: 'type', title: __('Type')},
                        {field: 'subject', title: __('Subject')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"1":'未支付',"2":'已支付',"3":'已退款'}, formatter: Table.api.formatter.status},
                        {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Controller.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
              avatar: function (value, row, index) {
                return '<img width="45" height="45" src="'+value+'" class="img-circle">';
              },
              operate: function (value, row, index) {
                var table = this.table;
                // 操作配置
                var options = table ? table.bootstrapTable('getOptions') : {};
                // 默认按钮组
                var buttons = $.extend([], this.buttons || []);
                if (options.extend.byOut_url !== ''){
                  buttons.push({
                    name: 'books',
                    icon: 'fa fa-paper-plane',
                    title: __('退款'),
                    text:'退款',
                    extend: 'data-toggle="tooltip"',
                    area:['50%', '80%'],
                    classname: 'btn btn-xs btn-success btn-out',
                    url: options.extend.byOut_url,
                    hidden:function (row) {
                      if(row.status == 1 || row.status == 3)
                      {
                        return true;
                      }
                      return false;
                    }
                  });
                }
                if (options.extend.edit_url !== ''){
                  buttons.push({
                    name: 'edit',
                    icon: 'fa fa-pencil',
                    title: __('Edit'),
                    extend: 'data-toggle="tooltip"',
                    classname: 'btn btn-xs btn-success btn-editone',
                    url: options.extend.edit_url
                  });
                }
                if (options.extend.del_url !== '') {
                  buttons.push({
                    name: 'del',
                    icon: 'fa fa-trash',
                    title: __('Del'),
                    extend: 'data-toggle="tooltip"',
                    classname: 'btn btn-xs btn-danger btn-delone',
                    url: options.extend.del_url
                  });
                }
                return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
              },
            }
        }
    };
    return Controller;
});